<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
<div id="wrapper wrapper-content">
    <form  class="form-horizontal m-t" id="form" th:action="@{/device/device/importDevice}"  method="POST"
          enctype="multipart/form-data">
        <div class="form-group">
            <label class="col-sm-3 control-label">
                选择文件：
                <span style="color: #e7262a">*</span>
            </label>
            <div class="col-sm-8">
                <input id="file" name="file" type="file" accept="*" required class="btn btn-default">
            </div>
            <!--<a class="btn btn-primary" onclick="submit()">提交</a>-->
        </div>
        <div class="form-group">
            <div class="col-sm-8 col-sm-offset-3">
                <a class="btn btn-primary" onclick="submit()">提交</a>
            </div>
        </div>
    </form>
</div>
<div th:include="include::footer"></div>
<script>

    function submit() {
        var fileObj = document.getElementById("file").files[0]; // 获取文件对象
        if (!fileObj) {
            layer.msg("请选择文件");
            return;
        }
        var fileNameArr = fileObj.name.split(".")
        console.log(fileNameArr)
        var fileName = fileNameArr[fileNameArr.length-1];
        if (fileName == 'xls' || fileName == 'xlsm'){
//            return true;
        }else{
            layer.msg("只支持xls或者xlsm为后缀的文件");
            return;
        }

        // FormData 对象
        var form = new FormData();
        // 文件对象
        form.append("file", fileObj);

        //加载层

        var index = layer.load(0, {shade: false}); //0代表加载的风格，支持0-2

        $.ajax({
            url: ctx + "/material/purchase/importPurchase",
            data: form,
            type: "POST",
            dataType: "json",
            cache: false,//上传文件无需缓存
            processData: false,//用于对data参数进行序列化处理 这里必须false
            contentType: false, //必须
            success: function (data) {
                if (data.code == 0) {
                    parent.layer.msg("操作成功");
                    parent.reLoad();
                    var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                    parent.layer.close(index);

                } else {
                    parent.layer.alert(data.msg)
                }
            },
            error: function (result) {
                parent.layer.alert("Connection error");
            }
        })
    }
</script>
</body>
</html>
